package org.example.struct.array;

import com.alibaba.fastjson2.JSON;

/**
 * @author liyishan
 * @date 2024/1/22 09:59
 * @apiNote
 */

public class Test00344ReverseString {
    public static void main(String[] args) {
        char[] s = {'h','e','l','l','o'};
        reverseString1(s);
        System.out.println(JSON.toJSON(s));
    }

    public static void reverseString(char[] s) {
        StringBuffer sb = new StringBuffer();
        for(int i = s.length-1; i >= 0; i--){
            sb.append(s[i]);
        }
        for(int i = 0; i < s.length; i++){
            s[i] = sb.charAt(i);
        }
    }

    public static void reverseString1(char[] s) {
        int n = s.length;
        for (int left = 0, right = n - 1; left < right; ++left, --right) {
            char tmp = s[left];
            s[left] = s[right];
            s[right] = tmp;
        }
    }
}
